﻿@using Gurux.DLMS.AMI.Shared.DIs

@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs
@using Gurux.DLMS.AMI.Shared.Rest
@using Gurux.DLMS.AMI.Shared.Enums
@using Gurux.DLMS.AMI.Client.Helpers
@using Gurux.DLMS.AMI.Module

@attribute [Authorize(Roles = "Admin, UserManager, User")]
@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IGXNotifier Notifier
@if (_active != null)
{
    <EditForm Model="@_active">
        <DataAnnotationsValidator />
        <ValidationSummary />
        <div class="row">
            <div style="width:100%">
                <div class="form-group">
                    <label>@Properties.Resources.Name:</label>
                    <input type="text" readonly="readonly" value="@_active.User?.UserName" class="form-control" />
                </div>
                <div class="form-group">
                    <label>@Properties.Resources.Generation:</label>
                    <input type="text" readonly="readonly" value="@_active.CreationTime" class="form-control" />
                </div>
                <div class="form-group">
                    <label>@Properties.Resources.Target:</label>
                    <input type="text" readonly="readonly" value="@_active.TargetType" class="form-control" />
                </div>
                <div class="form-group">
                    <label>@Properties.Resources.Action:</label>
                    <input type="text" readonly="readonly" value="@_active.Action" class="form-control" />
                </div>
                <div class="form-group">
                    <label>@Properties.Resources.Status:</label>
                    <input type="text" readonly="readonly" value="@_active.Status" class="form-control" />
                </div>
                <div class="form-group">
                    <label>@Properties.Resources.Data:</label>
                    <textarea id="Reply" @bind="_active.Data" rows="10" readonly="readonly" class="form-control" />
                </div>
                <div class="form-group">
                    <label>@Properties.Resources.Reply:</label>
                    <textarea id="Reply" @bind="_active.Reply" rows="10" readonly="readonly" class="form-control" />
                </div>
            </div>
        </div>
    </EditForm>
}
@code {
    /// <summary>
    /// Action.
    /// </summary>
    [Parameter]
    public string? Action { get; set; }

    /// <summary>
    /// Selected user action ID.
    /// </summary>
    [Parameter]
    public Guid? Id { get; set; }

    private GXUserAction? _active;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            if (Notifier == null)
            {
                throw new ArgumentException(Properties.Resources.InvalidNotifier);
            }
            Notifier.ProgressStart();
            Notifier.ClearStatus();
            //Get selected item.
            if (Id == null)
            {
                throw new ArgumentException(Properties.Resources.InvalidTarget);
            }
            Notifier.Clear();
            Notifier.AddMenuItem(new GXMenuItem() { Text = Properties.Resources.Close, Icon = "oi oi-action-undo", OnClick = OnCancel });
            Notifier.UpdateButtons();
            //Get user action data.
            HttpResponseMessage response = await Http.GetAsync(string.Format("api/UserAction?id={0}", Id));
            ClientHelpers.ValidateStatusCode(response);
            _active = (await response.Content.ReadFromJsonAsync<GetUserAction>())?.Item;
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
    }

    /// <summary>
    /// Cancel.
    /// </summary>
    private void OnCancel()
    {
        ClientHelpers.NavigateToLastPage(NavigationManager, Notifier);
    }
}
